
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>基础视图 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="通用显示视图" href="generic-display.html" />
    <link rel="prev" title="内置基于类的视图 API" href="index.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="内置基于类的视图 API">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="generic-display.html" title="通用显示视图">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-class-based-views-base">
            
  <div class="section" id="s-base-views">
<span id="base-views"></span><h1>基础视图<a class="headerlink" href="#base-views" title="永久链接至标题">¶</a></h1>
<p>以下三个类提供了创建 Django 视图所需的大部分功能。你可以把它们看作是 <em>父</em> 类的视图，它们可以自己使用，也可以从父类继承。它们可能无法提供项目所需的所有功能，在这种情况下，有基于类的混入和通用视图。</p>
<p>Django 内置的许多基于类的视图都是从其他基于类的视图或各种混入中继承过来的。因为这个继承链非常重要，所以祖先类被记录在 <strong>祖先（MRO）</strong> 节标题下。MRO 是方法解析顺序的缩写。</p>
<div class="section" id="s-view">
<span id="view"></span><h2><code class="docutils literal notranslate"><span class="pre">View</span></code><a class="headerlink" href="#view" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.base.View">
<em class="property">class </em><code class="descclassname">django.views.generic.base.</code><code class="descname">View</code><a class="headerlink" href="#django.views.generic.base.View" title="永久链接至目标">¶</a></dt>
<dd><p>基于主类的基本视图。所有其他基于类的视图都继承自这个基类。它并不是严格意义上的通用视图，因此也可以从 <code class="docutils literal notranslate"><span class="pre">django.views</span></code> 导入。</p>
<p><strong>方法流程图</strong></p>
<ol class="arabic simple">
<li><a class="reference internal" href="#django.views.generic.base.View.setup" title="django.views.generic.base.View.setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.http_method_not_allowed" title="django.views.generic.base.View.http_method_not_allowed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_method_not_allowed()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.options" title="django.views.generic.base.View.options"><code class="xref py py-meth docutils literal notranslate"><span class="pre">options()</span></code></a></li>
</ol>
<p><strong>示例 views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.views</span> <span class="kn">import</span> <span class="n">View</span>

<span class="k">class</span> <span class="nc">MyView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s1">&#39;Hello, World!&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>示例 urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">MyView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;mine/&#39;</span><span class="p">,</span> <span class="n">MyView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;my-view&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>属性</strong></p>
<dl class="attribute">
<dt id="django.views.generic.base.View.http_method_names">
<code class="descname">http_method_names</code><a class="headerlink" href="#django.views.generic.base.View.http_method_names" title="永久链接至目标">¶</a></dt>
<dd><p>此视图将接受的 HTT P方法名称列表。</p>
<p>默认：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="p">,</span> <span class="s1">&#39;post&#39;</span><span class="p">,</span> <span class="s1">&#39;put&#39;</span><span class="p">,</span> <span class="s1">&#39;patch&#39;</span><span class="p">,</span> <span class="s1">&#39;delete&#39;</span><span class="p">,</span> <span class="s1">&#39;head&#39;</span><span class="p">,</span> <span class="s1">&#39;options&#39;</span><span class="p">,</span> <span class="s1">&#39;trace&#39;</span><span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<p><strong>方法</strong></p>
<dl class="classmethod">
<dt id="django.views.generic.base.View.as_view">
<em class="property">classmethod </em><code class="descname">as_view</code>(<em>**initkwargs</em>)<a class="headerlink" href="#django.views.generic.base.View.as_view" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个接受请求并返回响应的可调用对象视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">response</span> <span class="o">=</span> <span class="n">MyView</span><span class="o">.</span><span class="n">as_view</span><span class="p">()(</span><span class="n">request</span><span class="p">)</span>
</pre></div>
</div>
<p>返回的视图有 <code class="docutils literal notranslate"><span class="pre">view_class</span></code> 和 <code class="docutils literal notranslate"><span class="pre">view_initkwargs</span></code> 属性。</p>
<p>当视图在请求／响应周期中被调用时， <a class="reference internal" href="#django.views.generic.base.View.setup" title="django.views.generic.base.View.setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup()</span></code></a> 方法将 <a class="reference internal" href="../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a> 分配给视图的 <code class="docutils literal notranslate"><span class="pre">request</span></code> 属性，将任何位置和／或关键字参数 <a class="reference internal" href="../../topics/http/urls.html#how-django-processes-a-request"><span class="std std-ref">从 URL 模式</span></a> 中捕获的参数分别分配给 <code class="docutils literal notranslate"><span class="pre">args</span></code> 和 <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> 属性。然后调用 <a class="reference internal" href="#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a>。</p>
</dd></dl>

<dl class="method">
<dt id="django.views.generic.base.View.setup">
<code class="descname">setup</code>(<em>request</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.base.View.setup" title="永久链接至目标">¶</a></dt>
<dd><p>在 <a class="reference internal" href="#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a> 之前执行关键视图初始化。</p>
<p>如果覆盖这个方法，必须调用 <code class="docutils literal notranslate"><span class="pre">super()</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.views.generic.base.View.dispatch">
<code class="descname">dispatch</code>(<em>request</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.base.View.dispatch" title="永久链接至目标">¶</a></dt>
<dd><p>视图的 <code class="docutils literal notranslate"><span class="pre">view</span></code> 部分——该方法接受 <code class="docutils literal notranslate"><span class="pre">request</span></code> 参数和其他参数，并返回 HTTP 响应。</p>
<p>默认的实现将检查 HTTP 方法，并尝试委托给与 HTTP 方法相匹配的方法；<code class="docutils literal notranslate"><span class="pre">GET</span></code> 将委托给 <code class="docutils literal notranslate"><span class="pre">get()</span></code>，<code class="docutils literal notranslate"><span class="pre">POST</span></code> 将委托给 <code class="docutils literal notranslate"><span class="pre">post()</span></code>，以此类推。</p>
<p>默认情况下，<code class="docutils literal notranslate"><span class="pre">HEAD</span></code> 请求将被委托给 <code class="docutils literal notranslate"><span class="pre">get()</span></code>。如果你需要以不同于 <code class="docutils literal notranslate"><span class="pre">GET</span></code> 的方式处理 <code class="docutils literal notranslate"><span class="pre">HEAD</span></code> 请求，你可以覆盖 <code class="docutils literal notranslate"><span class="pre">head()</span></code> 方法。参见 <a class="reference internal" href="../../topics/class-based-views/index.html#supporting-other-http-methods"><span class="std std-ref">支持其他 HTTP 方法</span></a> 的例子。</p>
</dd></dl>

<dl class="method">
<dt id="django.views.generic.base.View.http_method_not_allowed">
<code class="descname">http_method_not_allowed</code>(<em>request</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.base.View.http_method_not_allowed" title="永久链接至目标">¶</a></dt>
<dd><p>如果调用视图时使用了它不支持的 HTTP 方法，就会调用这个方法。</p>
<p>默认的实现返回 <code class="docutils literal notranslate"><span class="pre">HttpResponseNotAllowed</span></code>，其中包含一个纯文本的允许方法列表。</p>
</dd></dl>

<dl class="method">
<dt id="django.views.generic.base.View.options">
<code class="descname">options</code>(<em>request</em>, <em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.base.View.options" title="永久链接至目标">¶</a></dt>
<dd><p>处理响应 OPTIONS HTTP 动词的请求。返回一个包含 <code class="docutils literal notranslate"><span class="pre">Allow</span></code> 头的响应，该头包含一个视图允许的 HTTP 方法名称列表。</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-templateview">
<span id="templateview"></span><h2><code class="docutils literal notranslate"><span class="pre">TemplateView</span></code><a class="headerlink" href="#templateview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.base.TemplateView">
<em class="property">class </em><code class="descclassname">django.views.generic.base.</code><code class="descname">TemplateView</code><a class="headerlink" href="#django.views.generic.base.TemplateView" title="永久链接至目标">¶</a></dt>
<dd><p>渲染一个给定的模板，其上下文包含 URL 中捕获的参数。</p>
<p><strong>祖先（MRO）</strong></p>
<p>该视图从以下视图继承方法和属性。</p>
<ul class="simple">
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.ContextMixin" title="django.views.generic.base.ContextMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.ContextMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>方法流程图</strong></p>
<ol class="arabic simple">
<li><a class="reference internal" href="#django.views.generic.base.View.setup" title="django.views.generic.base.View.setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.http_method_not_allowed" title="django.views.generic.base.View.http_method_not_allowed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_method_not_allowed()</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.ContextMixin.get_context_data" title="django.views.generic.base.ContextMixin.get_context_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_context_data()</span></code></a></li>
</ol>
<p><strong>示例 views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.base</span> <span class="kn">import</span> <span class="n">TemplateView</span>

<span class="kn">from</span> <span class="nn">articles.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">HomePageView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>

    <span class="n">template_name</span> <span class="o">=</span> <span class="s2">&quot;home.html&quot;</span>

    <span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s1">&#39;latest_articles&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()[:</span><span class="mi">5</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">context</span>
</pre></div>
</div>
<p><strong>示例 urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">HomePageView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">HomePageView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;home&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>上下文</strong></p>
<ul class="simple">
<li>填充（通过 <a class="reference internal" href="mixins-simple.html#django.views.generic.base.ContextMixin" title="django.views.generic.base.ContextMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ContextMixin</span></code></a>）从服务于视图的 URL 模式中捕获的关键字参数。</li>
<li>你也可以使用 <a class="reference internal" href="mixins-simple.html#django.views.generic.base.ContextMixin.extra_context" title="django.views.generic.base.ContextMixin.extra_context"><code class="xref py py-attr docutils literal notranslate"><span class="pre">extra_context</span></code></a> 关键字参数为 <a class="reference internal" href="#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> 添加上下文。</li>
</ul>
</dd></dl>

</div>
<div class="section" id="s-redirectview">
<span id="redirectview"></span><h2><code class="docutils literal notranslate"><span class="pre">RedirectView</span></code><a class="headerlink" href="#redirectview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.base.RedirectView">
<em class="property">class </em><code class="descclassname">django.views.generic.base.</code><code class="descname">RedirectView</code><a class="headerlink" href="#django.views.generic.base.RedirectView" title="永久链接至目标">¶</a></dt>
<dd><p>重定向到给定的 URL。</p>
<p>给定的 URL 可能包含字典式的字符串格式，将对 URL 中捕获的参数进行插值。因为关键字插值 <em>总是</em> 进行的（即使没有参数传递进来），URL 中的任何 <code class="docutils literal notranslate"><span class="pre">&quot;%&quot;</span></code> 字符必须写成 <code class="docutils literal notranslate"><span class="pre">&quot;%%&quot;</span></code>，这样 Python 就会在输出中把它们转换为一个百分号。</p>
<p>如果给定的 UR L是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，Django 将返回一个 <code class="docutils literal notranslate"><span class="pre">HttpResponseGone</span></code> （410）。</p>
<p><strong>祖先（MRO）</strong></p>
<p>这个视图从以下的视图中继承了方法和属性：</p>
<ul class="simple">
<li><a class="reference internal" href="#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>方法流程图</strong></p>
<ol class="arabic simple">
<li><a class="reference internal" href="#django.views.generic.base.View.setup" title="django.views.generic.base.View.setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.dispatch" title="django.views.generic.base.View.dispatch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dispatch()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.View.http_method_not_allowed" title="django.views.generic.base.View.http_method_not_allowed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">http_method_not_allowed()</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.base.RedirectView.get_redirect_url" title="django.views.generic.base.RedirectView.get_redirect_url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_redirect_url()</span></code></a></li>
</ol>
<p><strong>示例 views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">get_object_or_404</span>
<span class="kn">from</span> <span class="nn">django.views.generic.base</span> <span class="kn">import</span> <span class="n">RedirectView</span>

<span class="kn">from</span> <span class="nn">articles.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">ArticleCounterRedirectView</span><span class="p">(</span><span class="n">RedirectView</span><span class="p">):</span>

    <span class="n">permanent</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">query_string</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">pattern_name</span> <span class="o">=</span> <span class="s1">&#39;article-detail&#39;</span>

    <span class="k">def</span> <span class="nf">get_redirect_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">article</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Article</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;pk&#39;</span><span class="p">])</span>
        <span class="n">article</span><span class="o">.</span><span class="n">update_counter</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_redirect_url</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>示例 urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">django.views.generic.base</span> <span class="kn">import</span> <span class="n">RedirectView</span>

<span class="kn">from</span> <span class="nn">article.views</span> <span class="kn">import</span> <span class="n">ArticleCounterRedirectView</span><span class="p">,</span> <span class="n">ArticleDetailView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;counter/&lt;int:pk&gt;/&#39;</span><span class="p">,</span> <span class="n">ArticleCounterRedirectView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;article-counter&#39;</span><span class="p">),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;details/&lt;int:pk&gt;/&#39;</span><span class="p">,</span> <span class="n">ArticleDetailView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;article-detail&#39;</span><span class="p">),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;go-to-django/&#39;</span><span class="p">,</span> <span class="n">RedirectView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="s1">&#39;https://www.djangoproject.com/&#39;</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;go-to-django&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>属性</strong></p>
<dl class="attribute">
<dt id="django.views.generic.base.RedirectView.url">
<code class="descname">url</code><a class="headerlink" href="#django.views.generic.base.RedirectView.url" title="永久链接至目标">¶</a></dt>
<dd><p>要重定向到的 URL，作为一个字符串。或者 <code class="docutils literal notranslate"><span class="pre">None</span></code> 引发 410（Gone）HTTP 错误。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.views.generic.base.RedirectView.pattern_name">
<code class="descname">pattern_name</code><a class="headerlink" href="#django.views.generic.base.RedirectView.pattern_name" title="永久链接至目标">¶</a></dt>
<dd><p>要重定向到的 URL 模式的名称。重定向将使用与此视图传递的相同的 args 和 kwargs 来完成。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.views.generic.base.RedirectView.permanent">
<code class="descname">permanent</code><a class="headerlink" href="#django.views.generic.base.RedirectView.permanent" title="永久链接至目标">¶</a></dt>
<dd><p>重定向是否应该是永久的。这里唯一的区别是返回的 HTTP 状态码。如果 <code class="docutils literal notranslate"><span class="pre">True</span></code>，那么重定向将使用状态码 301。如果 <code class="docutils literal notranslate"><span class="pre">False</span></code>，那么重定向将使用状态码 302。默认情况下，<code class="docutils literal notranslate"><span class="pre">permanent</span></code> 是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.views.generic.base.RedirectView.query_string">
<code class="descname">query_string</code><a class="headerlink" href="#django.views.generic.base.RedirectView.query_string" title="永久链接至目标">¶</a></dt>
<dd><p>是否将 GET 查询字符串传递到新位置。如果 <cite>True`</cite>，那么查询字符串将被附加到 URL 中。如果 <code class="docutils literal notranslate"><span class="pre">False</span></code>，那么查询字符串将被丢弃。默认情况下，<code class="docutils literal notranslate"><span class="pre">query_string</span></code> 是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<p><strong>方法</strong></p>
<dl class="method">
<dt id="django.views.generic.base.RedirectView.get_redirect_url">
<code class="descname">get_redirect_url</code>(<em>*args</em>, <em>**kwargs</em>)<a class="headerlink" href="#django.views.generic.base.RedirectView.get_redirect_url" title="永久链接至目标">¶</a></dt>
<dd><p>构建用于重定向的目标 URL。</p>
<p><code class="docutils literal notranslate"><span class="pre">args</span></code> 和 <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> 参数分别是位置参数和／或关键字参数 <a class="reference internal" href="../../topics/http/urls.html#how-django-processes-a-request"><span class="std std-ref">从 URL 模式</span></a> 中获取。</p>
<p>默认实现使用 <a class="reference internal" href="#django.views.generic.base.RedirectView.url" title="django.views.generic.base.RedirectView.url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">url</span></code></a> 作为起始字符串，并使用 URL 中捕获的命名组对该字符串中的 <code class="docutils literal notranslate"><span class="pre">%</span></code> 命名参数进行扩展。</p>
<p>如果没有设置 <a class="reference internal" href="#django.views.generic.base.RedirectView.url" title="django.views.generic.base.RedirectView.url"><code class="xref py py-attr docutils literal notranslate"><span class="pre">url</span></code></a>，<code class="docutils literal notranslate"><span class="pre">get_redirect_url()</span></code> 尝试使用 URL 中捕获的内容来反推 <a class="reference internal" href="#django.views.generic.base.RedirectView.pattern_name" title="django.views.generic.base.RedirectView.pattern_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">pattern_name</span></code></a> （命名组和未命名组都会使用）。</p>
<p>如果被 <a class="reference internal" href="#django.views.generic.base.RedirectView.query_string" title="django.views.generic.base.RedirectView.query_string"><code class="xref py py-attr docutils literal notranslate"><span class="pre">query_string</span></code></a> 请求，它也会将查询字符串附加到生成的 URL 中。子类可以实现任何他们想要的行为，只要该方法返回一个重定向就绪的 URL 字符串。</p>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">基础视图</a><ul>
<li><a class="reference internal" href="#view"><code class="docutils literal notranslate"><span class="pre">View</span></code></a></li>
<li><a class="reference internal" href="#templateview"><code class="docutils literal notranslate"><span class="pre">TemplateView</span></code></a></li>
<li><a class="reference internal" href="#redirectview"><code class="docutils literal notranslate"><span class="pre">RedirectView</span></code></a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="index.html"
                        title="上一章">内置基于类的视图 API</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="generic-display.html"
                        title="下一章">通用显示视图</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/class-based-views/base.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="内置基于类的视图 API">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="generic-display.html" title="通用显示视图">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>